<template>
  <div class="search-bar">
    <el-form :inline="true" :model="formItem" class="form-filter" size="mini">
      <el-form-item>
        <el-input v-model="formItem.s_name" clearable placeholder="请输入名称..." @keyup.enter.native="search" @change="onMixedInputChange" />
      </el-form-item>
      <el-form-item>
        <treeselect
          v-model="formItem.s_modelCode"
          :multiple="false"
          :options="cmdbModels"
          :load-options="loadOptions"
          :disable-branch-nodes="true"
          id-key="code"
          label-key="name"
          placeholder="请选择维护类别"
          @input="onSelectModelChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" plain icon="el-icon-search" @click="search" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { pureTree } from '@neu/dcom-ui/api/schema'

// 定义一个查询表单原始项
const formItem = {
  s_modelCode: null,
  s_name: ''
}

export default {
  name: 'MaintainRuleTableFilter',
  data() {
    return {
      /** CMDB模型树 */
      cmdbModels: null,
      formItem: {
        ...formItem
      },
      select: '',
      placeholder: ''
    }
  },
  mounted() {

  },
  methods: {
    // 查询按钮点击事件处理
    search: function() {
      this.$parent.filter(this.formItem)
    },
    reset: function() {
      this.formItem = {
        ...formItem
      }
      this.search()
    },
    onMixedInputChange(value) {
      if (value === '') {
        this.search()
      }
    },
    loadOptions({ action }) {
      if (action === 'LOAD_ROOT_OPTIONS') {
        return pureTree().then(response => {
          this.cmdbModels = response.data
        })
      }
    },
    onSelectModelChange() {
      this.search()
    }
  }
}
</script>
